package mps

import (
	"errors"
	"fmt"
	mts20140618 "github.com/alibabacloud-go/mts-20140618/client"
	"github.com/alibabacloud-go/tea/tea"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/util/gconv"
	"net/url"
)

type mediainfoService struct{}

type MediainfoResponseEntity struct {
	Width      uint    `json:"width,omitempty"`
	Height     uint    `json:"height,omitempty"`
	Duration   float32 `json:"duration,omitempty"`
	Fps        float32 `json:"fps,omitempty"`
	Bitrate    float32 `json:"bitrate,omitempty"`
	FileFormat string  `json:"file_format,omitempty"`
	FileSize   uint    `json:"file_size,omitempty"`
}

func (r *mediainfoService) Query(requestParam *mpsRequestEntity) (*mpsResponseEntity, error) {
	if requestParam == nil || requestParam.OssBucket == nil || requestParam.OssObject == nil {
		return nil, errors.New("mps invalid request param ,both bucket and object is nil")
	}

	// sdk 参数
	var submitMediaInfoJobRequest *mts20140618.SubmitMediaInfoJobRequest

	{
		ossObject := url.QueryEscape(Mps.cfg.OssLocation) // 指定位置
		requestParam.OssLocation = &ossObject
		// 参数
		requestInputBS, _ := gjson.Encode(requestParam)
		requestInput := gconv.String(requestInputBS)

		// 封装请求参数
		submitMediaInfoJobRequest = &mts20140618.SubmitMediaInfoJobRequest{
			Input:      &requestInput,
			PipelineId: &Mps.cfg.PipelineId,
			Async:      tea.Bool(false),
		}
	}

	client, _err := Mps.createClient()
	if _err != nil {
		return nil, _err
	}

	if respData, _err := client.SubmitMediaInfoJob(submitMediaInfoJobRequest); _err != nil {
		return nil, fmt.Errorf("mps submit mediainfo job failed,err: %s", _err.Error())
	} else {
		if respData.Body != nil && respData.Body.MediaInfoJob != nil {

			// 是否报错
			if respData.Body.MediaInfoJob.Code != nil {
				return nil, fmt.Errorf("mps query media info failed,err: %s", *respData.Body.MediaInfoJob.Message)
			}

			// 是否查询到参数
			if respData.Body.MediaInfoJob.Properties == nil {
				return nil, errors.New("mps query media info failed,err: no properties found")
			}

			// 结果参数
			var mediainfoResponseParam = new(MediainfoResponseEntity)
			// 文件属性判断
			{
				// 宽
				if respData.Body.MediaInfoJob.Properties.Width != nil {
					mediainfoResponseParam.Width = gconv.Uint(*respData.Body.MediaInfoJob.Properties.Width)
				}

				// 高
				if respData.Body.MediaInfoJob.Properties.Height != nil {
					mediainfoResponseParam.Height = gconv.Uint(*respData.Body.MediaInfoJob.Properties.Height)
				}

				// 时长
				if respData.Body.MediaInfoJob.Properties.Duration != nil {
					mediainfoResponseParam.Duration = gconv.Float32(*respData.Body.MediaInfoJob.Properties.Duration)
				}

				// fps
				if respData.Body.MediaInfoJob.Properties.Fps != nil {
					mediainfoResponseParam.Fps = gconv.Float32(*respData.Body.MediaInfoJob.Properties.Fps)
				}

				// 波特率
				if respData.Body.MediaInfoJob.Properties.Bitrate != nil {
					mediainfoResponseParam.Bitrate = gconv.Float32(*respData.Body.MediaInfoJob.Properties.Bitrate)
				}

				// 格式
				if respData.Body.MediaInfoJob.Properties.FileFormat != nil {
					mediainfoResponseParam.FileFormat = *respData.Body.MediaInfoJob.Properties.FileFormat
				}

				// 大小
				if respData.Body.MediaInfoJob.Properties.FileSize != nil {
					mediainfoResponseParam.FileSize = gconv.Uint(*respData.Body.MediaInfoJob.Properties.Format.Size)
				}

			}

			// 响应参数
			return &mpsResponseEntity{
				RequestId: *respData.Body.RequestId,
				Data:      mediainfoResponseParam,
			}, nil
		}
	}

	return nil, errors.New("mps failed to query media info")
}
